package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.GameAllRankingListDao;
import com.juyu.miliao.api.dao.mysql.GameAllRankingListLogDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.EnergyLevel;
import com.juyu.miliao.api.domain.GameManager;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.dto.GameAllRankingListDTO;
import com.juyu.miliao.api.domain.dto.GameAllRankingListLogDTO;
import com.juyu.miliao.api.domain.vo.GameManagerVO;
import com.juyu.miliao.api.domain.vo.GameRankingForSortVo;
import com.juyu.miliao.api.domain.vo.GameRankingListLogVO;
import com.juyu.miliao.api.domain.vo.GameRankingVo;
import com.juyu.miliao.api.service.cache.GameManagerCacheService;
import com.juyu.miliao.common.util.DateUtil;
import com.juyu.miliao.common.util.GameRankingListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Auther: 卜加超
 * @Date: 2019/8/20 14:36
 * @Description:
 */
@Service
@Slf4j
public class GameAllRankingListService {
    @Resource
    private GameAllRankingListDao gameAllRankingListDao;
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private GameManagerCacheService gameManagerCacheService;
    @Resource
    private GameAllRankingListLogDao gameAllRankingListLogDao;

    @Autowired
    private EnergyLevelService energyLevelService;

    public List<GameRankingVo> getGameAllRankingList(int type, LocalDate localDate) {
        List<GameAllRankingListDTO> list = new ArrayList<>();
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (type == 1) {
            list = gameAllRankingListDao.selectGameListByDay(localDate);
        } else if (type == 2) {
            list = gameAllRankingListDao.selectGameListByDay(localDate.minusDays(1));
        } else if (type == 3) {
            JSONObject weekByYearTime = DateUtil.getWeekByYearTime(localDate);
            LocalDate startTime = LocalDate.parse(weekByYearTime.get("startTime").toString(), dateFormatter);
            LocalDate endTime = LocalDate.parse(weekByYearTime.get("endTime").toString(), dateFormatter);
            list = gameAllRankingListDao.selectGameListByWeek(startTime, endTime);
        } else if (type == 4) {
            JSONObject weekByYearTime = DateUtil.getWeekByYearTime(localDate.minusWeeks(1));
            LocalDate startTime = LocalDate.parse(weekByYearTime.get("startTime").toString(), dateFormatter);
            LocalDate endTime = LocalDate.parse(weekByYearTime.get("endTime").toString(), dateFormatter);
            list = gameAllRankingListDao.selectGameListByWeek(startTime, endTime);
        }
        if (list == null && list.isEmpty()) {
            return new ArrayList<>();
        }
        List<GameRankingVo> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            GameRankingVo gameRankingVo = new GameRankingVo();
            User user = userInfoDao.getUserInfoById(list.get(i).getUserId());
            EnergyLevel energy = energyLevelService.getUserEnergyLevel(user.getEnergySum());
            gameRankingVo.setUserId(list.get(i).getUserId());
            gameRankingVo.setProfit(list.get(i).getReturnBean());
            gameRankingVo.setNiceName(user.getNickName());
            gameRankingVo.setIcon(user.getIcon());
            gameRankingVo.setIsVip(user.getIsVip());
            gameRankingVo.setIsSvip(user.getIsSvip());
            gameRankingVo.setLevelPic(energy.getLevelPic());
            if (type > 2) {
                gameRankingVo.setEstimatedRewardsBeans(GameRankingListUtil.getGameRankingRewardListByWeek().get(i));
            } else {
                gameRankingVo.setEstimatedRewardsBeans(GameRankingListUtil.getGameRankingRewardListByDay().get(i));
            }
            resultList.add(gameRankingVo);
        }
        return resultList;
    }

    public GameRankingForSortVo findMySort(int type, long id) {
        List<GameRankingVo> gameAllRankingList = getGameAllRankingList(type, LocalDate.now());
        GameRankingForSortVo gameRankingForSortVo = new GameRankingForSortVo();
        gameRankingForSortVo.setEstimatedRewardsBeans("--");
        gameRankingForSortVo.setSort("未上榜，加油哦！");
        for (int i = 0; i < gameAllRankingList.size(); i++) {
            gameRankingForSortVo.setProfit(gameAllRankingList.get(i).getProfit());
            if (id == gameAllRankingList.get(i).getUserId()) {
                gameRankingForSortVo.setEstimatedRewardsBeans(gameAllRankingList.get(i).getEstimatedRewardsBeans());
                gameRankingForSortVo.setSort("排名第" + (i + 1) + "名，再接再厉~");
            }
        }
        return gameRankingForSortVo;
    }

    public GameRankingForSortVo findMySortByMyProfit(long id) {
        List<GameRankingVo> gameAllRankingList = getGameAllRankingList(1, LocalDate.now());
        GameRankingForSortVo gameRankingForSortVo = new GameRankingForSortVo();
        GameAllRankingListDTO gameAllRankingListDTO = gameAllRankingListDao.selectGameListByUserId(id, LocalDate.now());
        if (gameAllRankingListDTO == null) {
            gameRankingForSortVo.setProfit(0L);
        } else {
            gameRankingForSortVo.setProfit(gameAllRankingListDTO.getReturnBean());
        }
        gameRankingForSortVo.setEstimatedRewardsBeans("无");
        gameRankingForSortVo.setSort("未上榜");
        for (int i = 0; i < gameAllRankingList.size(); i++) {
            if (id == gameAllRankingList.get(i).getUserId()) {
                gameRankingForSortVo.setEstimatedRewardsBeans(gameAllRankingList.get(i).getEstimatedRewardsBeans());
                gameRankingForSortVo.setSort("NO." + (i + 1));
            }
        }
        return gameRankingForSortVo;
    }

    public List<GameManagerVO> getGameList() {
        List<GameManagerVO> list = new ArrayList<>();
        List<GameManager> gameList = gameManagerCacheService.getGameList();
        for (GameManager game : gameList) {
            GameManagerVO gameManagerVO = new GameManagerVO();
            gameManagerVO.setGameName(game.getGameName());
            gameManagerVO.setPic(game.getPic());
            gameManagerVO.setOpenUrl(game.getOpenUrl());
            list.add(gameManagerVO);
        }
        return list;
    }

    public List<GameRankingListLogVO> getGameRankingListLog(long userId) {
        List<GameRankingListLogVO> list = new ArrayList<>();
        try {
            List<String> timeList = DateUtil.getStateTime();
//            List<GameRankingListLogVO> gameRankingListLogList =
//                    gameAllRankingListLogDao.getGameRankingListLog(userId, timeList.get(timeList.size() - 1), timeList.get(0));
//            Set<String> exSet = new HashSet<>();
//            for (String time : timeList) {
//                for (GameRankingListLogVO obj : gameRankingListLogList) {
//                    if (time.equals(obj.getRankingDate())) {
//                        list.add(obj);
//                        exSet.add(time);
//                    }
//                }
//                if (!exSet.contains(time)) {
//                    GameRankingListLogVO gameListLog = new GameRankingListLogVO();
//                    gameListLog.setRankingDate(time);
//                    gameListLog.setUserId(userId);
//                    gameListLog.setProfit(0L);
//                    gameListLog.setRewardsBeans("--");
//                    gameListLog.setSort("--");
//                    list.add(gameListLog);
//                }
//            }
            List<GameAllRankingListDTO> gameRankList = gameAllRankingListDao.selectGameListByUserIdAndTime(userId,
                    timeList.get(timeList.size() - 1), timeList.get(0));
            Set<String> exSet = new HashSet<>();
            for (String time : timeList) {
                for (GameAllRankingListDTO obj : gameRankList) {
                    if (time.equals(obj.getCreateDate().toString())) {

                        GameRankingListLogVO vo = new GameRankingListLogVO();
                        vo.setUserId(obj.getUserId());
                        vo.setProfit(obj.getReturnBean());
                        vo.setRankingDate(obj.getCreateDate().toString());
                        GameRankingListLogVO gameRankingListLog = gameAllRankingListLogDao
                                .getGameRankingListLogByUserIdAndTime(obj.getUserId(),obj.getCreateDate().toString());
                        if(gameRankingListLog==null){
                            vo.setSort("--");
                            vo.setRewardsBeans("--");
                        }else{
                            vo.setSort(gameRankingListLog.getSort());
                            vo.setRewardsBeans(gameRankingListLog.getRewardsBeans());
                        }
                        list.add(vo);
                        exSet.add(time);
                    }
                }
                if (!exSet.contains(time)) {
                    GameRankingListLogVO gameListLog = new GameRankingListLogVO();
                    gameListLog.setRankingDate(time);
                    gameListLog.setUserId(userId);
                    gameListLog.setProfit(0L);
                    gameListLog.setRewardsBeans("--");
                    gameListLog.setSort("--");
                    list.add(gameListLog);
                }
            }
        } catch (ParseException e) {
            log.error("往期回顾：" + e.getMessage());
        }
        return list;
    }
}
